package com.jh.paymentchannel.pojo;

import com.alipay.api.response.AlipayOpenAuthTokenAppResponse;
import com.google.common.base.MoreObjects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * @author Yichuan
 * @version 1.0
 */
@Entity
@Table(name="t_alipay_auth_token")
public class AlipayAuthToken implements Serializable {

    private static final long serialVersionUID = 2162535487466058259L;

    public static AlipayAuthToken create(String shopId, String appAuthCode,
                                         AlipayOpenAuthTokenAppResponse tokenAppResponse) {
        Date now = new Date();
        return new AlipayAuthToken(shopId, tokenAppResponse.getAuthAppId(), tokenAppResponse.getUserId(),
                appAuthCode, tokenAppResponse.getAppAuthToken(), tokenAppResponse.getAppRefreshToken(), now, now);
    }

    public static AlipayAuthToken newToken(String shopId) {
        AlipayAuthToken authToken = new AlipayAuthToken();
        authToken.setShopId(shopId);
        authToken.setEnabled(false);
        Date now = new Date();
        authToken.setUpdateTime(now);
        authToken.setCreateTime(now);
        return authToken;
    }

    public AlipayAuthToken updateQrcCode(String qrcCode) {
        setQrcCode(qrcCode);
        setExpiredTime(System.currentTimeMillis() + 60*1000);
        return this;
    }

    public AlipayAuthToken update(String appAuthCode, AlipayOpenAuthTokenAppResponse tokenAppResponse) {
        setAppAuthCode(appAuthCode);
        setAppId(tokenAppResponse.getAuthAppId());
        setPid(tokenAppResponse.getUserId());
        setAppAuthToken(tokenAppResponse.getAppAuthToken());
        setAppRefreshToken(tokenAppResponse.getAppRefreshToken());
        setUpdateTime(new Date());
        return this;
    }

    /**
     * 判断是否已授权
     */
    public boolean checkAuthToken() {
        return isEnabled() && getAuthEndTime() != null && getAuthEndTime().after(new Date());
    }

    /**
     * 判断是否需要刷新二维码
     * 前提是未授权状态下expiredTime==null或expiredTime<当前时间
     */
    public boolean needRefreshQrcCode() {
        return !checkAuthToken() && (getExpiredTime() == null || getExpiredTime() < System.currentTimeMillis());
    }

    @Id
    @Column(name = "shop_id")
    private String shopId;

    @Column(name = "app_id")
    private String appId;

    @Column(name = "p_id")
    private String pid;

    @Column(name = "app_auth_code")
    private String appAuthCode;

    @Column(name = "app_auth_token")
    private String appAuthToken;

    @Column(name = "app_refresh_token")
    private String appRefreshToken;

    @Column(name = "auth_start_time")
    private Date authStartTime;

    @Column(name = "auth_end_time")
    private Date authEndTime;

    @Column(name = "enabled")
    private boolean enabled;

    @Column(name = "qrc_code")
    private String qrcCode;

    @Column(name = "expired_time")
    private Long expiredTime;

    @Column(name = "create_time")
    private Date createTime;

    @Column(name = "update_time")
    private Date updateTime;

    public AlipayAuthToken() {
        super();
    }

    public AlipayAuthToken(String shopId, String appId, String pid, String appAuthCode, String appAuthToken,
                           String appRefreshToken, Date createTime, Date updateTime) {
        this.shopId = shopId;
        this.appId = appId;
        this.pid = pid;
        this.appAuthCode = appAuthCode;
        this.appAuthToken = appAuthToken;
        this.appRefreshToken = appRefreshToken;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }
    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getAppAuthCode() {
        return appAuthCode;
    }

    public void setAppAuthCode(String appAuthCode) {
        this.appAuthCode = appAuthCode;
    }

    public String getAppAuthToken() {
        return appAuthToken;
    }

    public void setAppAuthToken(String appAuthToken) {
        this.appAuthToken = appAuthToken;
    }

    public String getAppRefreshToken() {
        return appRefreshToken;
    }

    public void setAppRefreshToken(String appRefreshToken) {
        this.appRefreshToken = appRefreshToken;
    }

    public Date getAuthStartTime() {
        return authStartTime;
    }

    public void setAuthStartTime(Date authStartTime) {
        this.authStartTime = authStartTime;
    }

    public Date getAuthEndTime() {
        return authEndTime;
    }

    public void setAuthEndTime(Date authEndTime) {
        this.authEndTime = authEndTime;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getQrcCode() {
        return qrcCode;
    }

    public void setQrcCode(String qrcCode) {
        this.qrcCode = qrcCode;
    }

    public Long getExpiredTime() {
        return expiredTime;
    }

    public void setExpiredTime(Long expiredTime) {
        this.expiredTime = expiredTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AlipayAuthToken)) return false;
        AlipayAuthToken authToken = (AlipayAuthToken) o;
        return isEnabled() == authToken.isEnabled() &&
                Objects.equals(getShopId(), authToken.getShopId()) &&
                Objects.equals(getAppId(), authToken.getAppId()) &&
                Objects.equals(getPid(), authToken.getPid()) &&
                Objects.equals(getAppAuthCode(), authToken.getAppAuthCode()) &&
                Objects.equals(getAppAuthToken(), authToken.getAppAuthToken()) &&
                Objects.equals(getAppRefreshToken(), authToken.getAppRefreshToken()) &&
                Objects.equals(getAuthStartTime(), authToken.getAuthStartTime()) &&
                Objects.equals(getAuthEndTime(), authToken.getAuthEndTime()) &&
                Objects.equals(getQrcCode(), authToken.getQrcCode()) &&
                Objects.equals(getExpiredTime(), authToken.getExpiredTime()) &&
                Objects.equals(getCreateTime(), authToken.getCreateTime()) &&
                Objects.equals(getUpdateTime(), authToken.getUpdateTime());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getShopId(), getAppId(), getPid(), getAppAuthCode(), getAppAuthToken(),
                getAppRefreshToken(), getAuthStartTime(), getAuthEndTime(), isEnabled(), getQrcCode(),
                getExpiredTime(), getCreateTime(), getUpdateTime());
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("shopId", shopId)
                .add("appId", appId)
                .add("pid", pid)
                .add("appAuthCode", appAuthCode)
                .add("appAuthToken", appAuthToken)
                .add("appRefreshToken", appRefreshToken)
                .add("authStartTime", authStartTime)
                .add("authEndTime", authEndTime)
                .add("enabled", enabled)
                .add("qrcCode", qrcCode)
                .add("expiredTime", expiredTime)
                .add("createTime", createTime)
                .add("updateTime", updateTime)
                .toString();
    }
}
